<!DOCTYPE html>
<meta charset=utf-8>
<title>Syntax Highlighting</title>
<link href="../themes/css/blackboard.css" rel="stylesheet" type="text/css" media="screen">
<body>

<pre id="codeHighLightPer">
<code data-language="c" id ="codeHighLight">
/* variable names that match a k
eyword or type at the beginning */
char charcoal;
float interval;
char *floating;
double short_stuff, voider;

void floater(int x, int y)
{
    return;
}
</code>
</pre>

<pre>
<code data-language="c">
#include &lt;stdio.h&gt;
#include &lt;stdarg.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;unistd.h&gt;

#ifndef BUF_SIZE
#define BUF_SIZE 1024
#endif

static void die (const char * format, ...)
{
    va_list vargs;
    va_start(vargs, format);
    vfprintf(stderr, format, vargs);
    fprintf(stderr, ".\n");
    va_end(vargs);
    _exit(1);
}

int main (int argc, char *argv[])
{
    static int x;
    const float y;
    unsigned int n;
    unsigned short int g;
    char* test;

    int outFD, opt, openFlags = O_WRONLY;
    char buf[BUF_SIZE];
    ssize_t charCount;

    while ((opt = getopt(argc, argv, ":a")) != -1) {
	switch (opt) {
	case 'a':
	    openFlags |= O_APPEND;
	default:
	    die("Unrecognized option");
	}
    }

    outFD = open(argv[1], O_WRONLY);
    while ((charCount = read(STDIN_FILENO, buf, BUF_SIZE) > 0)) {
	if (charCount != write(STDOUT_FILENO, buf, BUF_SIZE))
	    die("Couldn't write same number of bytes to stdout");
	if (charCount != write(outFD, buf, BUF_SIZE))
	    die("Couldn't write same number of bytes to output file");
    }
    close(outFD);

    return 0;
}
</code>
</pre>
<code id="codeHighLight" data-language="c" class="rainbow rainbow-show"><lo><li><span class="meta preprocessor">#<span class="keyword define">ifndeftype_h</span></span></li>
<li><span class="meta preprocessor">#<span class="keyword define">define</span> <span class="entity name">type_h</span></span></li>
<span class="keyword">typedef</span> <span class="storage type">int</span> type_id;
<span class="meta preprocessor">#<span class="keyword define">define</span> <span class="entity name">typeid</span>(TYPE) type_find(#TYPE)</span>
<span class="meta preprocessor">#<span class="keyword define">define</span> <span class="entity name">type_name</span>(TYPE) #TYPE</span>
type_id <span class="entity name function">type_find</span>(<span class="storage type">const</span> <span class="storage type">char</span>* type);
<span class="storage type">const</span> <span class="storage type">char</span>* <span class="entity name function">type_id_name</span>(<span class="storage type">int</span> id);
<span class="meta preprocessor">#<span class="keyword define">endif</span></span></lo></code>
<pre>
<code data-language="c">
#ifndef type_h
#define type_h

typedef int type_id;

#define typeid(TYPE) type_find(#TYPE)
#define type_name(TYPE) #TYPE

type_id type_find(const char* type);
const char* type_id_name(int id);

#endif

</code>
</pre>

<pre>
<code data-language="c">
#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#include "error.h"

#include "type.h"

#define MAX_TYPE_LEN 512
#define MAX_NUM_TYPES 1024

/* Store Table of type names */
typedef char type_string[MAX_TYPE_LEN];
type_string type_table[MAX_NUM_TYPES];
int type_index = 0;

/* Dynamically enter types into table */
int type_find(const char* type) {

  if (strlen(type) >= MAX_TYPE_LEN) {
    error("Type name %s is too long to index into type table.", type);
  }
  if (type_index >= MAX_NUM_TYPES) {
    error("Too many types in type table already. Cannot add %s.", type);
  }

  for (int i = 0; i &lt; type_index; i++) {
    // Return type index if found
    if (strcmp(type, type_table[i]) == 0) {
      return i;
    }
  }

  // If not found add to table and return
  strcpy(type_table[type_index], type);
  type_index++;

  return type_index-1;
}

const char* type_id_name(int id) {
  return type_table[id];
}
</code>
</pre>

<pre>
<code data-language="c">
#ifndef sound_h
#define sound_h

#include "SDL/SDL.h"

typedef struct {
  char* data;
  int length;
} sound;

sound* wav_load_file(char* filename);
void sound_delete(sound* s);

#endif
</code>
</pre>

<pre>
<code data-language="c">
#include "error.h"

#include "assets/sound.h"

static void flip_endian(char* data, int length) {
  for(int i = 0; i &lt; length; i += 2) {
    int x = data[i];
    data[i] = data[i + 1];
    data[i + 1] = x;
  }
}

sound* wav_load_file(char* filename) {

  sound* s = malloc(sizeof(sound));

  SDL_AudioSpec spec;

  if( SDL_LoadWAV(filename, &amp;spec, (Uint8**)&amp;s->data, (Uint32*)&amp;s->length) == NULL) {
    error("Unable to load sound file %s", filename);
  }

  if ((spec.format != AUDIO_S16LSB) &amp;&amp;
      (spec.format != AUDIO_S16MSB)) {
    error("Unsupported sound format for file %s, id %i.", filename, spec.format);
  }

  if (spec.format != AUDIO_S16SYS) {
    flip_endian(s-&gt;data, s-&gt;length);
  }

  return s;
}

void sound_delete(sound* s) {
  SDL_FreeWAV((Uint8*)s-&gt;data);
  free(s);
}
</code>
</pre>

    <script src="../dist/rainbow.js"></script>
    <script src="../src/language/generic.js"></script>
    <script src="../src/language/c.js"></script>
</body>
